package cobersky.skinUI.components
{
	import cobersky.skinUI.core.Component;
	import cobersky.skinUI.skins.buttons.DefaultButtonSkin;
	import cobersky.skinUI.skins.core.IButtonSkin;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	import mx.states.AddChild;

	public class Button extends Component
	{
		private var label:TextField;
		private var upState:DisplayObject;
		private var downState:DisplayObject;
		private var overState:DisplayObject;
		private var _current:DisplayObject;
		private var _multiDispatch:Boolean;
		private var _multiDispatchDelay:int;

		public function get multiDispatchDelay():int
		{
			return _multiDispatchDelay;
		}

		public function set multiDispatchDelay(value:int):void
		{
			_multiDispatchDelay = value;
		}

		public function get multiDispatch():Boolean
		{
			return _multiDispatch;
		}

		public function set multiDispatch(value:Boolean):void
		{
			_multiDispatch = value;
			if(value&&!_multiDispatchDelay)_multiDispatchDelay=2;
		}

		override public function create():void{
			if(!_skinClass)_skinClass=DefaultButtonSkin;
			attachSkin();
			addChild(_current);
			drawHitarea();
			this.addEventListener(MouseEvent.ROLL_OVER,eventHandler);
			this.addEventListener(MouseEvent.ROLL_OUT,eventHandler);
			this.addEventListener(MouseEvent.MOUSE_DOWN,eventHandler);
			this.addEventListener(MouseEvent.MOUSE_UP,eventHandler);
			this.addEventListener(Event.ENTER_FRAME,dispatch);
		}
		
		override public function set height(value:Number):void
		{
			overState.height=downState.height=_current.height=value;
			drawHitarea();
		}
		
		override public function set width(value:Number):void
		{
			overState.width=downState.width=upState.width=value;
			drawHitarea();
		}
		override public function attachSkin():void{
			super.attachSkin();
			var b:IButtonSkin=new _skinClass();
			_current=upState=b.upStateSkin;
			downState=b.downStateSkin;
			overState=b.overStateSkin;
		}
		protected function drawHitarea():void{
			mouseChildren=false;
			var r:Rectangle=upState.getRect(this);
			graphics.clear();
			graphics.beginFill(0,0);
			graphics.drawRect(r.x,r.y,r.width,r.height);
			graphics.endFill();
		}
		private var _canDispatch:Boolean;
		private var _delayTimer:int;
		
		protected function eventHandler(event:MouseEvent):void{
			switch(event.type){
				case MouseEvent.MOUSE_DOWN:
					if(!_canDispatch){
						this._delayTimer=_multiDispatchDelay;
						this._canDispatch=true;
						this.removeChild(_current);
						_current=downState;
						addChild(_current);
					}
					break;
				case MouseEvent.ROLL_OUT:
					this.removeChild(_current);
					_current=upState;
					addChild(_current);
					this._canDispatch=false;
					break;
				case MouseEvent.ROLL_OVER:
					this.removeChild(_current);
					_current=overState;
					addChild(_current);
					
					break;
				case MouseEvent.MOUSE_UP:
					this.removeChild(_current);
					_current=overState;
					addChild(_current);
					this._canDispatch=false;
					break;
				default:
					break;
			}
		}
		
		protected function dispatch(event:Event):void
		{
			if(_multiDispatch&&_canDispatch){
				if(_delayTimer<0){
					_delayTimer=_multiDispatchDelay;
					this.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN));
				}
				_delayTimer--;
			}
		}
		override public function dispose():void{
			super.dispose();
		}
	}
}